Program cdlist;
uses dos;
    {creates a cd list text file from a directory listing of the cd}

 Function poslast(ch:char;s:string):byte;
 {returns the last posision of 'ch' found in 's'}
    var i:integer;
    begin
    poslast:=0;
    if s<>'' then
    for i:=1 to length(s) do
    if s[i]=ch then poslast:=i;
    end;

function char2char(ch,ch2:char;s:string):string;
var i:integer;
begin
    for i:=1 to length(s) do
    if s[i]=ch then s[i]:=ch2;
    char2char:=s;
end;

function DelStr(StrTodel,s:string):string;
var PosFound,i:integer;
begin
     Repeat
           PosFound:=pos(StrTodel,s);
           if PosFound>0 then delete(s,PosFound,Length(StrToDel));
     Until PosFound=0;
     DelStr:=s;
end;

function add_brackets_dash(s:string):string;
var i,i1,i2:integer;
begin
i:=poslast(')',s);
i1:=poslast('(',s);
i2:=poslast('-',s);

{Brackets after dash change them to curly}
if (i>0) and (i2>0) and (i1>0) and (i>i2) then
   begin
   s[i]:='}';
   s[i1]:='{';
   end;

i:=pos(')',s);
i2:=pos('-',s);
{if no brackets, insert them}
if (i=0) and (i2>0) then
   begin
   insert(')',s,i2);
   insert('(',s,1);
   end;
{if no dash, insert it}
if (i>0) and (i2=0) then insert('-',s,i+1);

add_brackets_dash:=s;
end;

function del_spaces_from_wrong_places(s:string):string;
var temp,ch:char;
    i:integer;
    found:boolean;
begin

    {del double spaces}

    repeat
    found:=false;
    temp:='A';
    for i:=1 to length(s) do
    begin
    ch:=s[i];
    if (s[i]=' ') and (s[i]=temp) then  begin delete(s,i-1,1); found:=true; end;
    temp:=ch;
    end;
    until not found;

    {del spaces after and before [,]}
    if ( (pos('(',s)-1)>0 ) and (s[pos('(',s)-1]=' ') then delete(s,pos('(',s)-1,1);
    if ( (pos('(',s)+1)>0 ) and (s[pos('(',s)+1]=' ') then delete(s,pos('(',s)+1,1);
    if ( (pos(')',s)-1)>0 ) and (s[pos(')',s)-1]=' ') then delete(s,pos(')',s)-1,1);
    del_spaces_from_wrong_places:=s;
end;

function add_spaces_in_right_places(s:string):string;
var  i,j:integer;
begin
    j:=length(s);
    if j>2 then
    for i:=2 to j do
    begin
    if (s[i]='-') and (s[i+1]<>' ') then
       begin insert(' ',s,i+1); inc(j); end;
    if (s[i]='-') and (s[i-1]<>' ') then
       begin insert(' ',s,i); inc(j); end;
    end;
add_spaces_in_right_places:=s;
end;

Function lowcase(s:string):string;
    var i:integer;
    begin
    if s<>'' then
    for i:=1 to length(s) do
    if (s[i]>='A') and (s[i]<='Z') then s[i]:=chr(ord(s[i])+32);
    lowcase:=s;
    end;

Function upcase_first_letters(s:string):string;
    var i:integer;
    begin
    if (s[1]>='a') and (s[1]<='z') then s[1]:=upcase(s[1]);
    if length(s)>2 then
    for i:=2 to length(s) do
    if (s[i]>='a') and (s[i]<='z') then
    if (s[i-1]=' ') or (s[i-1]='{') or (s[i-1]='(') or (s[i-1]='.') then s[i]:=upcase(s[i]);
    upcase_first_letters:=s;
    end;

Function strip_path(s:string;var p:string):string;
begin
p:=copy(s,1,poslast('\',s));
strip_path:=copy(s,poslast('\',s)+1,length(s)-poslast('\',s));
end;

var f2,f:text;
    s,p:string;
    l,max:integer;
begin
     max:=0;
     if not (paramcount<1) then
     begin
       Assign(f,paramstr(1));
       reset(f);
       Assign(f2,paramstr(1)+'.bat');
       rewrite(f2);
       while not eof(f) do
       begin
       readln(f,s);
       if max<length(strip_path(s,p)) then max:=length(strip_path(s,p));
       end;
       Max:=Max+5;
       reset(f);
       while not eof(f) do
       begin
       readln(f,s);
       s:=strip_path(s,p);
       writeln(f2,'@cd "'+p+'"');
       write(f2,'@Rename "'+s+'" ');
       l:=length(p+s);
       while l<max do begin write(f2,' '); inc(l); end;
       s:=char2char('_',' ',s);
       s:=char2char('[','(',s);
       s:=char2char(']',')',s);
       s:=add_brackets_dash(s);
       s:=del_spaces_from_wrong_places(s);
       s:=add_spaces_in_right_places(s);
       s:=lowcase(s);
       s:=Upcase_first_letters(s);
       s:=DelStr('(',s);
       s:=DelStr(')',s);
       writeln(f2,'"'+s+'"');
       end;
     end
     else
     begin
      Writeln('***Cd song name beautyfier 0.9b*** by Aurora Technologies');
      Writeln('Usage:   SongName Dirlisting');
      Writeln('Example: SongName dirlist');
     end;
     close(f);
     close(f2);
end.
